<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <!-- https://blog.csdn.net/fu983531588/article/details/89454446 -->
</head>

<body>
  <div id="app">
    用户名:<input type="text" v-model="uname" v-focus><br>
    <span>{{tip}}</span>
  </div>
  <script src="../js/vue.js"></script>
  <script>
    const vm = new Vue({
      "el": "#app",
      "data": {
        "uname": "",
        "tip": ""
      },
      "methods": {
        checkName(uname) {
          let timeOutId = window.setTimeout(() => { //使用定时器模拟后台接口
            if (uname === "admin") {
              this.tip = "用户名已经被注册了，请更换一个";
            } else {
              this.tip = "用户名可以使用";
            };
          }, 2000);
        }
      },
      "directives": {
        "focus": {
          inserted(el) {
            el.focus();
          }
        }
      },
      "computed": {},
      "watch": {
        uname(val, oldVal) {
          // 调用后台接口
          this.checkName(val);
          // 修改提示信息
          this.tip = "正在验证...";
        }
      }
    });
  </script>
</body>

</html>